package com.wang.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.controller.MyTest;
import com.wang.mapper.TbPlaybackMapper;
import com.wang.pojo.Live;
import com.wang.pojo.TXPlayback;
import com.wang.pojo.TbPlayback;
import com.wang.response.ResultResponse;
import com.wang.service.LiveService;
import com.wang.mapper.LiveMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class LiveServiceImpl
    implements LiveService{
    @Autowired
    LiveMapper liveMapper;

    @Override
    public ResultResponse addLive(Live live) {
        //生成推流地址
        Date startTime =    live.getStartTime();
        startTime.setDate(startTime.getDate()+1);
        String txSecret1 = MyTest.getSafeUrl("tSPSZWcQhZDxwGCbXMsW",
                live.getSpecialistId() + "",
                startTime.getTime() / 1000);
        String url="webrtc://txtl.yjs1906wyx.xyz/live/"+live.getSpecialistId()+"?"+txSecret1;
        live.setLiveUrl(url);
        //生成拨流地址
        Date endTime = live.getEndTime();
        endTime.setDate(endTime.getDate()+1);
        String txSecret2 = MyTest.getSafeUrl("AytxeybWxYNxNajZw7Dm", live.getSpecialistId() + "", endTime.getTime() / 1000);
        String playUrl="webrtc://txbl.yjs1906wyx.xyz/live/"+live.getSpecialistId()+"?"+txSecret2;
        live.setPlayUrl(playUrl);
        liveMapper.insert(live);
        return ResultResponse.SUCCESS(live.getLiveId());
    }

    @Override
    public ResultResponse getLiveUrlBySpecialistId(Integer specialistId) {
        QueryWrapper<Live> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Live::getSpecialistId,specialistId).eq(Live::getLiveStatus,0);
        queryWrapper.lambda().orderByAsc(Live::getStartTime).last("limit 1");
        Live live = liveMapper.selectOne(queryWrapper);
        //已开播
       // live.setLiveStatus(1);
        //liveMapper.updateById(live);
        return ResultResponse.SUCCESS(live);
    }
    @Autowired
    TbPlaybackMapper playbackMapper;

    @Override
    public String callback(String json) {
        System.out.println(json);
        TXPlayback txPlayback = JSON.parseObject(json, TXPlayback.class);
        System.out.println(txPlayback);
        TbPlayback tbPlayback = new TbPlayback();
        tbPlayback.setSpecialistId(Integer.valueOf(txPlayback.getStream_id()));
        tbPlayback.setFileFormat(txPlayback.getFile_format());
        tbPlayback.setDuration(txPlayback.getDuration());
        tbPlayback.setVideoUrl(txPlayback.getVideo_url());
        playbackMapper.insert(tbPlayback);
        return "{'code':'200'}";
    }

    @Override
    public ResultResponse getLiveUrlByLivId(Integer liveId) {
        Live live = liveMapper.selectById(liveId);
        live.setLiveStatus(1);
        liveMapper.updateById(live);
        return ResultResponse.SUCCESS(live);
    }

    @Override
    public ResultResponse finishLive(Integer liveId) {
        Live live = liveMapper.selectById(liveId);
        live.setLiveStatus(2);
        liveMapper.updateById(live);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse showLiveAll(Integer pageNum, Integer pageSize) {
        Page<Live> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Live> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByAsc(Live::getLiveStatus);
        Page<Live> page1 = liveMapper.selectPage(page, queryWrapper);

        return ResultResponse.SUCCESS(page1.getRecords(),page1.getTotal());
    }

    @Override
    public ResultResponse seeReplay(Integer specialistId) {
        QueryWrapper<TbPlayback> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbPlayback::getSpecialistId,specialistId);
        queryWrapper.lambda().orderByDesc(TbPlayback::getCreateTime);
        List<TbPlayback> playbackList = playbackMapper.selectList(queryWrapper);
        return ResultResponse.SUCCESS(playbackList);
    }


}




